package jeni2.VCD;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class viewallvcd extends Activity{
	private EditText tkode,tjudul,tkategory,thargasewa;
	private Button update,delete;
	private Long mRowId;
	private buatdatabase dbhelper;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		dbhelper = new buatdatabase(this);
		dbhelper.open();
		
		setContentView(R.layout.formviewallvcd);
		
		tkode = (EditText) findViewById(R.id.tkode);
		tjudul = (EditText) findViewById(R.id.tjudul);
		tkategory = (EditText) findViewById(R.id.tkategory);
		thargasewa = (EditText) findViewById(R.id.thargasewa);
		update = (Button) findViewById(R.id.update);
		delete = (Button) findViewById(R.id.Delete);
		
		mRowId = savedInstanceState != null ? savedInstanceState.getLong(buatdatabase.KEY_ROWID) : null;
		
		if(mRowId == null)
		{
			Bundle extras = getIntent().getExtras();
			mRowId = extras != null ? extras.getLong(buatdatabase.KEY_ROWID) : null;
		}
		
		populateFields();
		tkode.setEnabled(false);
		delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try
				{
					dbhelper.deleteAddress(Long.parseLong(tkode.getText().toString()));
					Toast.makeText(getBaseContext(), "Data have been successfully added", Toast.LENGTH_SHORT).show();
				}
				catch(Exception e)
				{
					Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
				}
				setResult(RESULT_OK);
				finish();	
				
			}
		});
		update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String judul = tjudul.getText().toString();
				String kategori = tkategory.getText().toString();
				String hargasewa = thargasewa.getText().toString();
				if(judul.equals("") || kategori.equals("") || hargasewa.equals("")){
					Toast.makeText(getBaseContext(), "Masih ada yang kosong", Toast.LENGTH_SHORT).show();
				}else{
				try
				{
					dbhelper.updateAddress(Long.parseLong(tkode.getText().toString()), judul,kategori,hargasewa);
					Toast.makeText(getBaseContext(), "Berhasil di update", Toast.LENGTH_SHORT).show();
				}
				catch(Exception e)
				{
					Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
				}
				setResult(RESULT_OK);
				finish();
			}
				
			}
		});
	}
	private void populateFields() {
		if(mRowId != null)
		{
			Cursor address = dbhelper.fetchAddress(mRowId);
			startManagingCursor(address);
			
			/*address.moveToFirst();
	        while (address.isAfterLast() == false) {
	            name.append("n" + address.getString(2));
	       	    address.moveToNext();
	        }
	        address.close();*/
			
			tjudul.setText(address.getString(address.getColumnIndexOrThrow(buatdatabase.KEY_judul)));
			tkode.setText(address.getString(address.getColumnIndexOrThrow(buatdatabase.KEY_ROWID)));
			tkategory.setText(address.getString(address.getColumnIndexOrThrow(buatdatabase.KEY_kategori)));
			thargasewa.setText(address.getString(address.getColumnIndexOrThrow(buatdatabase.KEY_hargasewa)));
		}
		
	}
}
